package ysq.xyg.web.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.service.impl.BaseServiceImpl;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.web.dao.WebConfigDao;
import ysq.xyg.web.model.po.WebConfig;
import ysq.xyg.sys.model.po.SysUser;
import ysq.xyg.web.model.vo.WebConfigVo;
import ysq.xyg.web.service.WebConfigService;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


/**
 * 网站模块-网站配置service实现
 *
 * @author shengQiang yu
 * @date 2019-6-5 21:54:50
 */
@Service("webConfigService")
public class WebConfigServiceImpl extends BaseServiceImpl<WebConfig> implements WebConfigService {

    @Resource
    private WebConfigDao webConfigDao;

    private WebConfig webConfig;

    @Autowired
    public void setBaseDao(WebConfigDao webConfigDao) {
        this.baseDao = webConfigDao;
    }


    /**
     * 分页查询
     *
     * @param webConfigVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-6-5 21:54:50
     */
    public ReturnEntity page(WebConfigVo webConfigVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy(" update_time desc");
        List<WebConfig> list = acceptList(webConfigVo);
        PageInfo<WebConfig> page=new PageInfo<WebConfig>(list);
        return new ReturnEntity(page.getList(),page.getTotal(),page.isHasNextPage());
    }

    /**
     * 修改
     *
     * @param webConfigVo 对象
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-6-5 21:54:50
     */
    public ReturnEntity addOrUpdate(WebConfigVo webConfigVo, SysUser currentUser) {
        boolean isUpate=true;
        if(StrUtil.isEmpty(webConfigVo.getId())){
            webConfig=new WebConfig(currentUser);
            isUpate=false;
        }else{
            webConfig=selectByPrimaryKey(webConfigVo.getId());
            if(ObjectUtil.isNull(webConfig)){
                return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
            }
            webConfig.setUpdateTime(new Date());
            webConfig.setUpdateUser(currentUser.getId());
        }
        webConfig.setName(webConfigVo.getName());
        webConfig.setSiteUrl(webConfigVo.getSiteUrl());
        webConfig.setSiteStyle(webConfigVo.getSiteStyle());
        webConfig.setCopyright(webConfigVo.getCopyright());
        webConfig.setCoverImage(webConfigVo.getCoverImage());
        webConfig.setSeoKeyword(webConfigVo.getSeoKeyword());
        webConfig.setDescription(webConfigVo.getDescription());
        webConfig.setSiteNumber(webConfigVo.getSiteNumber());
        webConfig.setTelephone(webConfigVo.getTelephone());
        webConfig.setServicePhone(webConfigVo.getServicePhone());
        webConfig.setWechat(webConfigVo.getWechat());
        webConfig.setQq(webConfigVo.getQq());
        webConfig.setEmail(webConfigVo.getEmail());
        webConfig.setAddress(webConfigVo.getAddress());
        webConfig.setUserId(currentUser.getId());
        int num=0;
        if(isUpate){
            num=updateByPrimaryKey(webConfig);
        }else{
            num=insert(webConfig);
        }
        if(num<=0){
            return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
        }
        return new ReturnEntity();
    }
    /**
     * 删除
     *
     * @param id 主键id
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-6-5 21:54:50
     */
    public ReturnEntity delete(String id, SysUser currentUser) {
        WebConfig webConfig=selectByPrimaryKey(id);
        if(webConfig!=null){
            webConfig.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_DELETE);
            webConfig.setUpdateTime(new Date());
            webConfig.setUpdateUser(currentUser.getId());
            int num=updateByPrimaryKey(webConfig);
            if(num>0){
                return new ReturnEntity();
            }
        }
        return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
    }

    /**
     * 获取集合
     *
     * @param webConfigVo 对象
     * @return List<BaseDict> BaseDict集合
     * @author shengQiang yu
     * @date 2019-6-5 21:54:50
     */
    public List<WebConfig> acceptList(WebConfigVo webConfigVo){
        Example example = new Example(WebConfig.class);
        Example.Criteria criteria = example.createCriteria();
        if(StringUtil.isNotEmpty(webConfigVo.getRemoveTag())){
            criteria.andEqualTo("removeTag",webConfigVo.getRemoveTag());
        }
        example.setOrderByClause(" update_time desc");
        return webConfigDao.selectByExample(example);
    }
}
